package com.alex.core.config;

import com.alex.core.mq.RocketMQProducer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.oss.OSSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.Properties;

/**
 * @author alex
 */
@Configuration
public class CoreFactory {

    @Autowired
    private CoreProperties coreProperties;

    /**
     * mq生产着
     *
     * @return
     */
    @Bean(initMethod = "start", destroyMethod = "shutdown", name = "rocketMQProducer")
    @Lazy
    public RocketMQProducer rocketMQProducer() {
        RocketMQProducer bean = new RocketMQProducer(coreProperties.getRocketTopic());
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.AccessKey, coreProperties.getRocketAccessKey());
        properties.put(PropertyKeyConst.SecretKey, coreProperties.getRocketSecretKey());
        properties.put(PropertyKeyConst.NAMESRV_ADDR, coreProperties.getOnsAddr());
        properties.put(PropertyKeyConst.INSTANCE_ID, coreProperties.getRocketInstanceId());
        bean.setProperties(properties);
        return bean;
    }

    //阿里云oss
    @Bean
    public OSSClient getOSSClient() {
        return new OSSClient(coreProperties.getOssEndpoint(), coreProperties.getOssAccessKeyId(), coreProperties.getOssAccessKeySecret());
    }
}
